|
|
|
|
|
|
|
NT, you will only have the code in the #ElseIf Win32 section of the statement block. This special version of the statement is typically used to compile the same application for different operating systems. |
|
|
|
|
|
|
|
|
The Select Case Statement |
|
|
|
|
|
|
|
|
The Select Case statement is another way to test for multiple conditions and then execute the related program code. However, it will provide a much easier section of code to maintain. The following is the syntax of the statement: |
|
|
|
|
|
|
|
|
Select Case testexpression
[Case expressionlist-n
[statements-n]]
[Case Else
[elsestatements]]
End Select |
|
|
|
|
|
|
|
|
If you take a moment to look at this syntax, you will probably feel like you have seen it somewhere before. In fact, if you have played with the Application Wizard and have added a toolbar to the application, the wizard uses a Select Case statement to test for the button that was clicked, as shown in the following: |
|
|
|
|
|
|
|
|
Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)
On Error Resume Next
Select Case Button.Key
Case Open
ToDo: Add Open button code.
MsgBox Add Open button code.
Case Bold
ToDo: Add Bold button code.
MsgBox Add Bold button code.
Case Underline
ToDo: Add Underline button code.
MsgBox Add Underline button code.
End Select
End Sub |
|
|
|
|
|
|
|
|
Just like with the If-Then statement, the statements that can be executed in the Case block can be a mixture of any standard Visual Basic statements, including other Select statements. In the Select statement, an expression is evaluated in the opening Select Case statement, then the Case statements within the Select-End Select block test the resulting value against one or more values or expressions. For example, if you want to perform one section of code if an expression is zero, another if it is odd, and a third section of code if it's even, with a maximum value of 10, you could code an If statement with an elaborate expression to figure out whether the number is odd or even, or you could use the Select Case statement as shown here: |
|
|
|
|
|